﻿using System.Collections.Generic;
using System.Net;
using System.Text;
using Comunication;
using ServerDomain.CommandFolder;
using Utiles;

namespace ServerDomain
{
    public class UdpPacketProcessor : IUdpPacketProcessor
    {
        public void ProcessPacket(byte[] buffer, IPEndPoint remoteEndpoint)
        {
            UTF8Encoding decoder = new UTF8Encoding();
            string header = decoder.GetString(buffer, 0, 3);
            string cmd = decoder.GetString(buffer, 3, 2);
            int dataLength = int.Parse(decoder.GetString(buffer, 5, 5));
            string data = decoder.GetString(buffer, 10, dataLength);

            Commands command = (Commands)int.Parse(cmd);

            remoteEndpoint.Port = Properties.Settings.Default.UdpPort;
            int bandwith;

            Command commandToSend;
            switch (command)
            {
                case Commands.UPLOAD_CLIENT_SERVER_FIND_SERVER:
                    if (ServerInfo.Instance.ServerIsAdequate())
                    {
                        ServerInfo.Instance.ChangeServerState(ServerStates.SERVER_UPLAOD);
                        commandToSend = new UploadServerClientFindServerResponse();
                        UDPClient.SendUdpMessage(remoteEndpoint, commandToSend);
                    }
                    break;
                case Commands.SERVER_SERVER_FIND_SERVER:
                    ServerInfo.Instance.ChangeServerState(ServerStates.SERVER_SYNC_FILE);
                    bandwith = int.Parse(Utilidades.ObtenerInformacion("BANDWITH", data, ';'));
                    ServerInfo.Instance.AddServer(remoteEndpoint,new ServerDTO(){Bandwith = bandwith, Ip = remoteEndpoint});
                    commandToSend = new ServerServerNotifyBandwith();
                    UDPClient.SendUdpMessage(remoteEndpoint, commandToSend);
                    break;
                case Commands.SERVER_SERVER_NOTIFY_BANDWITH:
                    ServerInfo.Instance.ChangeServerState(ServerStates.SERVER_SYNC_BANDWITH);
                    bandwith = int.Parse(Utilidades.ObtenerInformacion("BANDWITH", data, ';'));
                    ServerInfo.Instance.UpdateServerBandwith(remoteEndpoint,bandwith);
                    break;
                case Commands.DOWNLOAD_CLIENT_SERVER_FIND_SERVER:
                    if (header.Equals("REQ"))
                    {
                        ServerInfo.Instance.ChangeServerState(ServerStates.SERVER_DOWNLOAD);
                        var fileName = Utilidades.ObtenerInformacion("FILENAME", data, ';');
                        var bytesProcesados = Utilidades.ObtenerInformacion("BYTES", data, ';');
                        KeyValuePair<string, string>? archivo = ServerInfo.Instance.ExistsFile(fileName);
                        DownloadServerClientFindServerResponse downloadServerClientFindServerResponse = new DownloadServerClientFindServerResponse(archivo, bytesProcesados);
                        UDPClient.SendUdpMessage(remoteEndpoint, downloadServerClientFindServerResponse);
                    }
                    break;
                    
            }
        }

    }
}
 